﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.BaseService.Contract.Studio;
using DWF.Studio.Custom;
using DWF.Studio.Project;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.BaseService
{
    internal class ProjectService : IProjectService
    {
        private ProjectManager _projectManager = null;

        public ProjectService()
        {
            _projectManager = ProjectManager.Instance;
        }

        public bool CheckProjectOpened()
        {
            return _projectManager.CurrentProject != null;
        }

        public string ImportXamlFile(string sourceFile)
        {
            var ext = Path.GetExtension(sourceFile);

            if (String.IsNullOrWhiteSpace(ext) || ext != StaticResource.XamlExtension)
            {
                throw new ArgumentException();
            }

            var forder = _projectManager.GetProjectFolder(StaticResource.ProjectSelfFile);
            var file = _projectManager.CopyFileToProject(forder, sourceFile);
            if (file != null)
            {
                GlobalActionHandler.ScriptShow?.Invoke(new TagEventArgs<Project.ProjectFile>(file));
            }

            return file?.FullName;
        }

        public string NewFlowchartFile(string fileName)
        {
            var forder = _projectManager.GetProjectFolder(StaticResource.ProjectSelfFile);
            ProjectFile file = null;
            if (_projectManager.CurrentProject.WFLanguage == StaticResource.WorkflowLanguage_vb)
            {
                file = _projectManager.CreateXamlFileToProject(forder, fileName, StaticResource.XamlFileTemplateFlowchart_vb);
            }
            else if (_projectManager.CurrentProject.WFLanguage == StaticResource.WorkflowLanguage_cs)
            {
                file = _projectManager.CreateXamlFileToProject(forder, fileName, StaticResource.XamlFileTemplateFlowchart_cs);
            }

            if (file != null)
            {
                GlobalActionHandler.ScriptShow?.Invoke(new TagEventArgs<Project.ProjectFile>(file));
            }

            return file?.FullName;
        }

        public string NewSequenceFile(string fileName)
        {
            var forder = _projectManager.GetProjectFolder(StaticResource.ProjectSelfFile);
            ProjectFile file = null;
            if (_projectManager.CurrentProject.WFLanguage == StaticResource.WorkflowLanguage_vb)
            {
                file = _projectManager.CreateXamlFileToProject(forder, fileName, StaticResource.XamlFileTemplateSequence_vb);
            }
            else if (_projectManager.CurrentProject.WFLanguage == StaticResource.WorkflowLanguage_cs)
            {
                file = _projectManager.CreateXamlFileToProject(forder, fileName, StaticResource.XamlFileTemplateSequence_cs);
            }

            if (file != null)
            {
                GlobalActionHandler.ScriptShow?.Invoke(new TagEventArgs<Project.ProjectFile>(file));
            }

            return file?.FullName;
        }
    }
}
